import 'package:lighthouse/index.dart';

class WorkOrderPlfilePage extends StatefulWidget {
  late String orderId;
  late String taskId;

  WorkOrderPlfilePage({Key? key, required this.orderId, required this.taskId})
      : super(key: key);

  @override
  _WorkOrderPlfileState createState() => _WorkOrderPlfileState();
}

class _WorkOrderPlfileState extends State<WorkOrderPlfilePage> {
  late List<String> list = ["满意", "不满意", "需跟进"];
  late int satisfy = 0;
  late TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    // TODO: implement build
    final textScaleFactor = Global.textScaleFactor(context);
    return GlobalStyle(
      child: AlertDialog(
        title: Text('确认提示'),
        content: SingleChildScrollView(
          child: SizedBox(
            height: 250 * textScaleFactor,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text("确认工单是否归档？",
                    style: themeData.textTheme.titleMedium
                        ?.copyWith(color: themeData.colorScheme.onPrimary)),
                Padding(
                  padding: EdgeInsets.only(top: 10, bottom: 10),
                  child: Text("优质服务：",
                      style: themeData.textTheme.titleSmall
                          ?.copyWith(color: themeData.colorScheme.onPrimary)),
                ),
                Row(
                  children: [
                    SizedBox(
                      width: 20,
                      height: 20,
                      child: Checkbox(
                        value: satisfy == 0,
                        activeColor: themeData.primaryColor, //选中时的颜色
                        onChanged: (value) {
                          Debug.info("-----$value");
                          setState(() {
                            satisfy = 0;
                          });
                        },
                      ),
                    ),
                    const SizedBox(
                      width: 5,
                    ),
                    Expanded(
                      child: Text(
                        list[0],
                        style: themeData.textTheme.bodyMedium?.copyWith(
                            color: themeData.colorScheme.onSecondary),
                      ),
                    ),
                    const SizedBox(
                      width: 5,
                    ),
                    SizedBox(
                      width: 20,
                      height: 20,
                      child: Checkbox(
                        value: satisfy == 1,
                        activeColor: themeData.primaryColor, //选中时的颜色
                        onChanged: (value) {
                          Debug.info("-----$value");
                          setState(() {
                            satisfy = 1;
                          });
                        },
                      ),
                    ),
                    const SizedBox(
                      width: 5,
                    ),
                    Expanded(
                      child: Text(
                        list[1],
                        style: themeData.textTheme.bodyMedium?.copyWith(
                            color: themeData.colorScheme.onSecondary),
                      ),
                    ),
                    const SizedBox(
                      width: 5,
                    ),
                    SizedBox(
                      width: 20,
                      height: 20,
                      child: Checkbox(
                        value: satisfy == 2,
                        activeColor: themeData.primaryColor, //选中时的颜色
                        onChanged: (value) {
                          Debug.info("-----$value");
                          setState(() {
                            satisfy = 2;
                          });
                        },
                      ),
                    ),
                    const SizedBox(
                      width: 5,
                    ),
                    Expanded(
                      child: Text(
                        list[2],
                        style: themeData.textTheme.bodyMedium?.copyWith(
                            color: themeData.colorScheme.onSecondary),
                      ),
                    ),
                  ],
                ),
                Padding(
                  padding: EdgeInsets.only(top: 10, bottom: 10),
                  child: Text("归档说明：",
                      style: themeData.textTheme.titleSmall
                          ?.copyWith(color: themeData.colorScheme.onPrimary)),
                ),
                Expanded(
                    child: TextField(
                  controller: _textEditingController,
                  keyboardType: TextInputType.multiline,
                  textInputAction: TextInputAction.done,
                  maxLength: 100,
                  maxLines: 5,
                  decoration: InputDecoration(
                      // labelText: content,
                      border: const OutlineInputBorder(),
                      contentPadding: const EdgeInsets.all(8),
                      hintText: "请输入归档说明"),
                )),
              ],
            ),
          ),
        ),
        actions: <Widget>[
          TextButton(
            child: const Text('取消'),
            onPressed: () {
              Navigator.of(context).pop('cancel');
            },
          ),
          TextButton(
            child: const Text('确认'),
            onPressed: () {
              if (_textEditingController.text.isEmpty) {
                //不能为空
                showToast("归档说明不能为空");
              } else {
                TaskRequest taskRequest = TaskRequest();
                taskRequest.businessFrom = {
                  "regularState": satisfy,
                  "regularMemo": _textEditingController.text
                };
                taskRequest.operationType = "agree";
                taskRequest.taskId = widget.taskId;
                _handelOrder(taskRequest);
              }
            },
          ),
        ],
      ),
    );
  }

  void _handelOrder(TaskRequest taskRequest) async {
    showLoading(context);
    try {
      ResponseData responseData =
          await Api(context).handleOrder(taskRequest: taskRequest);
      //关闭当前页面
      Navigator.of(context).pop();

      if (responseData.success) {
        eventBus.fire(OrderEvent(EventType.edit, Order()..id = widget.orderId));
        await showToast(responseData.msg);
        Navigator.popUntil(context, ModalRoute.withName("WorkOrderDetails"));
        Navigator.of(context).pushReplacement(MaterialPageRoute(
            settings: const RouteSettings(name: "WorkOrderDetails"),
            builder: (context) =>
                WorkOrderDetailsPage(orderId: widget.orderId)));
      } else {
        showToast(responseData.msg);
      }
    } on DioError catch (e) {
      Debug.info("0000$context");
      Navigator.of(context).pop();
      //失败则提示
      showToast(e.response?.statusMessage ?? S.of(context).networkError);
    } on Error catch (e) {
      Navigator.of(context).pop();
      showToast(Global.isRelease ? S.of(context).networkError : e.toString());
    }
  }
}
